package com.qishi.exampleFour;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.DynamicMethodMatcherPointcut;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class GreetingDynamicPointcut extends DynamicMethodMatcherPointcut {



    private static List<String> specialNames = new ArrayList();


    static {
        specialNames.add("John");
        specialNames.add("Tom");
    }

    //对类进行静态切点检查
    @Override
    public ClassFilter getClassFilter() {
        return new ClassFilter() {
            public boolean matches(Class<?> aClass) {
                System.out.println("调用getClassFilter()对"+aClass.getName()+"做类级别的静态检查");
                return Waiter.class.isAssignableFrom(aClass);
            }
        };
    }

    //方法上的静态切点检查
    @Override
    public boolean matches(Method method, Class<?> targetClass) {

        System.out.println("调用matches(method,clazz)"+targetClass.getName()+"."+method.getName()+"做方法级别的静态检查");
        return "greetTo".equals(method.getName());
    }

    //方法上的动态切点检查
    public boolean matches(Method method, Class<?> aClass, Object... objects) {
        System.out.println("调用matches(method,clazz)"+aClass.getName()+"."+method.getName()+"做方法级别的动态检查");
        String clientName = (String) objects[0];

        return specialNames.contains(clientName);
    }
}
